////  表情键盘[练习]
//  YAEmoticonText.swift

//
//  Created by 鱼龙之梦 on 15/9/12.
//  Copyright © 2015年 panyanzhi. All rights reserved.
//

import UIKit

class YAEmoticonText: NSTextAttachment {
    var chs:String?
    init(chs:String){
        self.chs = chs
        super.init(data: nil, ofType: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    class func emoticonAttributeText(emoticon:YAEmoticon,font:UIFont) -> NSAttributedString{
        let attachment = YAEmoticonText(chs: emoticon.chs!)
        attachment.image = UIImage(contentsOfFile: emoticon.imagePath)
        //设置图片的行高
        let height = font.lineHeight
        //bounds 的x/y 就是scrollView的contentOffset，苹果利用bounds的x/y能够调整控件内部的偏移量
        attachment.bounds = CGRect(x: 0, y: -4, width: height, height: height)
        //create picture attribution str
        let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
        //添加默认字体
        imageText.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        return imageText
        
    }
}
